/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.guaraba.controlepatrimonio.model.dao.impl;

import br.com.guaraba.controlepatrimonio.model.Instituicao;
import br.com.guaraba.controlepatrimonio.model.dao.IDaoInstituicao;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

/**
 *
 * @author marcelo
 */
public class DaoInstituicaoImpl implements IDaoInstituicao{
    EntityManagerFactory emf;
    
    public DaoInstituicaoImpl(EntityManagerFactory emf) {
        this.emf = emf;
    }
    
    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    @Override
    public void create(Instituicao instituicao) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            em.persist(instituicao);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    @Override
    public void edit(Instituicao instituicao) throws Exception  {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            instituicao = em.merge(instituicao);
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Long id = instituicao.getId();
                if (find(id) == null) {
                    throw new Exception("The instituicao with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    @Override
    public void destroy(Instituicao instituicao) throws Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Instituicao instituicaoEncontrada;
            try {
                instituicaoEncontrada = em.getReference(Instituicao.class, instituicao.getId());
                instituicao.getId();
            } catch (Exception enfe) {
                throw new Exception("The instituicao with id " + instituicao.getId() + " no longer exists.", enfe);
            }
            em.remove(instituicao);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    @Override
    public List<Instituicao> findEntities() {
        return findEntities(true, -1, -1);
    }

    @Override
    public List<Instituicao> findEntities(int maxResults, int firstResult) {
        return findEntities(false, maxResults, firstResult);
    }

    private List<Instituicao> findEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Instituicao.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }        
    }
    
    @Override
    public Instituicao find(Long id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Instituicao.class, id);
        } finally {
            em.close();
        }        
    }

    @Override
    public int getCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Instituicao> rt = cq.from(Instituicao.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }        
    
    
}
